/**
 * 
 */
package com.lt.flashcard.model.service;

import com.google.gdata.client.contacts.*;
import com.google.gdata.client.http.AuthSubUtil;
import com.google.gdata.data.contacts.ContactEntry;
import com.google.gdata.data.contacts.ContactFeed;
import com.google.gdata.util.*;
import com.lt.flashcard.Utility;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.List;

/**
 * @author Administrator
 *
 */
public class ImportGoogleContactService {
	
	private final String IMPORT_CONTACTS_GOOGLE_REDIRECT_URL = Utility.getConfiguration("com.lt.flashcard.importContactGoogleRedirectURL","http://localhost:8089/flashcard/import_contacts.zul");
	private final String IMPORT_CONTACTS_GOOGLE_SCOPE = Utility.getConfiguration("com.lt.flashcard.importContactGoogleScope","http://www.google.com/m8/feeds/");
	private final String IMPORT_CONTACTS_GOOGLE_APP_NAME = Utility.getConfiguration("com.lt.flashcard.importContactGoogleAppName","flashcard");
	private final boolean SECURE = false;
	private final boolean SESSION = true;
	private String authSubLogin;
	private String token;
	private String sessionToken;
	private ContactsService contactService;
	
	public ImportGoogleContactService() {
		try {
			contactService = new ContactsService(IMPORT_CONTACTS_GOOGLE_APP_NAME);
			authSubLogin = AuthSubUtil.getRequestUrl(IMPORT_CONTACTS_GOOGLE_REDIRECT_URL, IMPORT_CONTACTS_GOOGLE_SCOPE, SECURE, SESSION);
			token = AuthSubUtil.getTokenFromReply(authSubLogin);
			sessionToken = AuthSubUtil.exchangeForSessionToken(token, null);
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (GeneralSecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<ContactEntry> getContactsFromGoogleAcc(String email, String password) {
		try {
			contactService.setAuthSubToken(sessionToken);
			contactService.setUserCredentials(email, password);
			URL feedUrl = new URL(IMPORT_CONTACTS_GOOGLE_SCOPE + "contacts/" + email + "/full");
			ContactFeed resultFeed = contactService.getFeed(feedUrl, ContactFeed.class);
			return resultFeed.getEntries();
		} catch (AuthenticationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * @return the authSubLogin
	 */
	public String getAuthSubLogin() {
		return authSubLogin;
	}


	/**
	 * @param authSubLogin the authSubLogin to set
	 */
	public void setAuthSubLogin(String authSubLogin) {
		this.authSubLogin = authSubLogin;
	}


	/**
	 * @return the token
	 */
	public String getToken() {
		return token;
	}


	/**
	 * @param token the token to set
	 */
	public void setToken(String token) {
		this.token = token;
	}


	/**
	 * @return the sessionToken
	 */
	public String getSessionToken() {
		return sessionToken;
	}


	/**
	 * @param sessionToken the sessionToken to set
	 */
	public void setSessionToken(String sessionToken) {
		this.sessionToken = sessionToken;
	}


	/**
	 * @return the contactService
	 */
	public ContactsService getContactService() {
		return contactService;
	}


	/**
	 * @param contactService the contactService to set
	 */
	public void setContactService(ContactsService contactService) {
		this.contactService = contactService;
	}
}
